package leetcode_core.leetcode_1;

import java.util.ArrayList;
import java.util.List;

public class Subsets {
    private List<List<Integer>> ans = new ArrayList<List<Integer>>();

    public List<List<Integer>> subsets(int[] nums) {
        dfs(nums.length-1,nums);
        return ans;
    }
    private List<Integer> copy(List<Integer> raw,int n){
        List<Integer> newList = new ArrayList<>();
        for (Integer id : raw) {
            newList.add(id);
        }
        newList.add(n);
        return newList;
    }
    //递归计算
    private void dfs(int cur,int[] nums){
        if(cur == -1){
            //此时为空集
            ans.add(new ArrayList<>());
            return;
        }
        //先计算出之前所有元素的子集
        int n = nums[cur];
        dfs(cur-1,nums);
        //然后在先前的基础上追加即可
        int size = ans.size();
        for(int i = 0;i<size;i++){
            List<Integer> copy = copy(ans.get(i), n);
            ans.add(copy);
        }
    }


}
